using System;
using System.Configuration;
using StructureMap;

namespace CxRomos.Core.Services.Implementation {
    [Pluggable("Default")]
    public class Configuration : IConfiguration {
        private static object GetAppSetting(Type expectedType, string key) {
            var value = ConfigurationManager.AppSettings.Get(key);
            if(value == null) {
                throw new Exception(string.Format("AppSetting: {0} is not configured", key));
            }
            try {
                if(expectedType.Equals(typeof(int))) {
                    return int.Parse(value);
                }
                if(expectedType.Equals(typeof(string))) {
                    return value;
                }
                throw new Exception("Not supported");
            }
            catch(Exception) {
                throw new Exception(string.Format("Config key: {0} was expected to be of type {1} but it was not", key, expectedType));
            }
        }

        public string SiteName {
            get { return GetAppSetting(typeof(string), "SiteName").ToString(); }
        }

        public string RootUrl {
            get { return GetAppSetting(typeof(string), "RootURL").ToString(); }
        }

    }
}